package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.ProductType;
import com.yc.supermarket.service.ProductTypeService;
import com.yc.supermarket.service.impl.ProductTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/productType")
public class ProductTypeController extends BaseController {

    private ProductTypeService service = new ProductTypeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if("findAll".equals(op)){
            doFindAll(req,resp);
        }else if("findTypeById".equals(op)){
            doFindTypeById(req,resp);
        }else {
            printToJson(resp,"No method.....405");
        }
    }

    protected void doFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Object> map = service.findAll();
        printToJson(resp,map);
    }

    protected void doFindTypeById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProductType type = parseRequest(req, ProductType.class);
        Map<String,Object> map = service.findTypeById(type);
        printToJson(resp,map);
    }
}
